module.exports = app => {
  const router = require("express").Router();
  const mongoose = require("mongoose");
  const Category = require("../models/Category");
  const Shoe = require("../models/Shoe");

  router.get("/shoes/init", async (req, res) => {
    console.log("/shoes/init", req.sesssion);
    // const shoesList = [
    //   {
    //     img:
    //       "https://fs2.bonbons.com.tw/upload/ftp/360Photo/300134090-360-1.jpg",
    //     name: "puma",
    //     size: 39,
    //     bianhao: "R24-26 STREETBALL 简版小椰子700 白灰黑EF9597 26"
    //   },
    //   {
    //     img:
    //       "https://fs2.bonbons.com.tw/upload/ftp/360Photo/300134090-360-1.jpg",
    //     name: "ad",
    //     size: 40,
    //     bianhao: "R24-26 STREETBALL 简版小椰子700 白灰黑EF9597 26"
    //   },
    //   {
    //     img:
    //       "https://fs2.bonbons.com.tw/upload/ftp/360Photo/300134090-360-1.jpg",
    //     name: "n",
    //     size: 41,
    //     bianhao: "R24-26 STREETBALL 简版小椰子700 白灰黑EF9597 26"
    //   },
    //   {
    //     img:
    //       "https://fs2.bonbons.com.tw/upload/ftp/360Photo/300134090-360-1.jpg",
    //     name: "nick",
    //     size: 42,
    //     bianhao: "R24-26 STREETBALL 简版小椰子700 白灰黑EF9597 26"
    //   },
    //   {
    //     img:
    //       "https://fs2.bonbons.com.tw/upload/ftp/360Photo/300134090-360-1.jpg",
    //     name: "sss",
    //     size: 43,
    //     bianhao: "R24-26 STREETBALL 简版小椰子700 白灰黑EF9597 26"
    //   }
    // ];
    // await Shoe.deleteMany({});
    // await Shoe.insertMany(shoesList);
    res.send("init");
  });

  router.get("/shoes/brands", async (req, res) => {
    // const ShoesList = await Shoe.find();
    // let brandsList = await ShoesList.map(el => {
    //   return el.brand;
    // });
    let brandsList = await Shoe.find(
      { sold_num: { $gt: 0 }, qty: { $gt: 0 } },
      { brand: 1, _id: 0 }
    );
    brandsList = brandsList.map(el => {
      return el.brand;
    });
    brandsList = Array.from(new Set(brandsList));
    // console.log(brandsList);
    console.log("/shoes/brands", req.sesssion);
    res.send(brandsList);
  });
  router.get("/shoes/sizes", async (req, res) => {
    const brand = req.query.brand;
    const shoesList = await Shoe.find({
      brand: brand,
      sold_num: { $gt: 0 },
      qty: { $gt: 0 }
    });
    let sizesList = shoesList.map(el => {
      return el.size;
    });
    sizesList = Array.from(new Set(sizesList)).sort();
    console.log("/shoes/sizes", req.sesssion);
    res.send(sizesList);
  });

  router.get("/shoes/list", async (req, res) => {
    console.log(req.query);
    const ShoesList = await Shoe.find({
      brand: req.query.brand,
      size: req.query.size,
      sold_num: { $gt: 0 },
      qty: { $gt: 0 }
    });
    // console.log(ShoesList);
    console.log("/shoes/list", req.sesssion);
    res.send(ShoesList);
  });

  router.get("/detail", async (req, res) => {
    const detail = await Shoe.find(req.query);
    console.log(req.query);
    console.log("/detail", req.sesssion);
    res.send(detail[0]);
  });

  app.use("/web/api", router);
  app.use((req, res, next) => {
    next();
  });
};
